{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-alpha0\n",
      "sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)\n",
      "matplotlib 3.0.3\n",
      "numpy 1.16.2\n",
      "pandas 0.24.2\n",
      "sklearn 0.20.3\n",
      "tensorflow 2.0.0-alpha0\n",
      "tensorflow.python.keras.api._v2.keras 2.2.4-tf\n"
     ]
    }
   ],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sklearn\n",
    "import pandas as pd\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import tensorflow as tf\n",
    "\n",
    "from tensorflow import keras\n",
    "\n",
    "print(tf.__version__)\n",
    "print(sys.version_info)\n",
    "for module in mpl, np, pd, sklearn, tf, keras:\n",
    "    print(module.__name__, module.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. _california_housing_dataset:\n",
      "\n",
      "California Housing dataset\n",
      "--------------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      "    :Number of Instances: 20640\n",
      "\n",
      "    :Number of Attributes: 8 numeric, predictive attributes and the target\n",
      "\n",
      "    :Attribute Information:\n",
      "        - MedInc        median income in block\n",
      "        - HouseAge      median house age in block\n",
      "        - AveRooms      average number of rooms\n",
      "        - AveBedrms     average number of bedrooms\n",
      "        - Population    block population\n",
      "        - AveOccup      average house occupancy\n",
      "        - Latitude      house block latitude\n",
      "        - Longitude     house block longitude\n",
      "\n",
      "    :Missing Attribute Values: None\n",
      "\n",
      "This dataset was obtained from the StatLib repository.\n",
      "http://lib.stat.cmu.edu/datasets/\n",
      "\n",
      "The target variable is the median house value for California districts.\n",
      "\n",
      "This dataset was derived from the 1990 U.S. census, using one row per census\n",
      "block group. A block group is the smallest geographical unit for which the U.S.\n",
      "Census Bureau publishes sample data (a block group typically has a population\n",
      "of 600 to 3,000 people).\n",
      "\n",
      "It can be downloaded/loaded using the\n",
      ":func:`sklearn.datasets.fetch_california_housing` function.\n",
      "\n",
      ".. topic:: References\n",
      "\n",
      "    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\n",
      "      Statistics and Probability Letters, 33 (1997) 291-297\n",
      "\n",
      "(20640, 8)\n",
      "(20640,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import fetch_california_housing\n",
    "\n",
    "housing = fetch_california_housing()\n",
    "print(housing.DESCR)\n",
    "print(housing.data.shape)\n",
    "print(housing.target.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(11610, 8) (11610,)\n",
      "(3870, 8) (3870,)\n",
      "(5160, 8) (5160,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "x_train_all, x_test, y_train_all, y_test = train_test_split(\n",
    "    housing.data, housing.target, random_state = 7)\n",
    "x_train, x_valid, y_train, y_valid = train_test_split(\n",
    "    x_train_all, y_train_all, random_state = 11)\n",
    "print(x_train.shape, y_train.shape)\n",
    "print(x_valid.shape, y_valid.shape)\n",
    "print(x_test.shape, y_test.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "scaler = StandardScaler()\n",
    "x_train_scaled = scaler.fit_transform(x_train)\n",
    "x_valid_scaled = scaler.transform(x_valid)\n",
    "x_test_scaled = scaler.transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(9.0, shape=(), dtype=float32)\n",
      "tf.Tensor(5.0, shape=(), dtype=float32)\n",
      "tf.Tensor(5.0, shape=(), dtype=float32)\n",
      "tf.Tensor(4.0, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# metric使用\n",
    "\n",
    "metric = keras.metrics.MeanSquaredError()\n",
    "print(metric([5.], [2.]))\n",
    "print(metric([0.], [1.]))\n",
    "print(metric.result())\n",
    "\n",
    "metric.reset_states()\n",
    "metric([1.], [3.])\n",
    "print(metric.result())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 0  train mse: 2.3031447\t valid mse:  1.6836152221107805\n",
      "Epoch 1  train mse: 1.2553148  train mse: 1.2026824 1  train mse: 1.219423 1  train mse: 1.2235386\t valid mse:  1.5200177376959239\n",
      "Epoch 2  train mse: 1.2695357 2  train mse: 1.2168293 2  train mse: 1.2634597\t valid mse:  1.488341353576161\n",
      "Epoch 3  train mse: 1.3547979\t valid mse:  1.471280505433066\n",
      "Epoch 4  train mse: 1.3345486\t valid mse:  1.4617474514496056\n",
      "Epoch 5  train mse: 1.32495275  train mse: 1.3080626 1.3262509\t valid mse:  1.4499624458013085\n",
      "Epoch 6  train mse: 1.3306845\t valid mse:  1.4440280431635029\n",
      "Epoch 7  train mse: 1.2839619\t valid mse:  1.4389758052560142\n",
      "Epoch 8  train mse: 1.3365296\t valid mse:  1.4311768609466622\n",
      "Epoch 9  train mse: 1.3375881\t valid mse:  1.4273971610343115\n",
      "Epoch 10  train mse: 1.2988005\t valid mse:  1.4218589447652488\n",
      "Epoch 11  train mse: 1.3097074 train mse: 1.292158\t valid mse:  1.4185611865544907\n",
      "Epoch 12  train mse: 1.2856087\t valid mse:  1.4160330016480376\n",
      "Epoch 13  train mse: 1.2999125 13  train mse: 1.2591456\t valid mse:  1.4129535019135646\n",
      "Epoch 14  train mse: 1.3240643\t valid mse:  1.4120439509125213\n",
      "Epoch 15  train mse: 1.2922549\t valid mse:  1.4086217970602449\n",
      "Epoch 16  train mse: 1.3106796\t valid mse:  1.4066873648386988\n",
      "Epoch 17  train mse: 1.2598072\t valid mse:  1.4077383432763233\n",
      "Epoch 18  train mse: 1.2767592\t valid mse:  1.4050346660594262\n",
      "Epoch 19  train mse: 1.3298701\t valid mse:  1.4024417644803846\n",
      "Epoch 20  train mse: 1.3062184\t valid mse:  1.4025270039257318\n",
      "Epoch 21  train mse: 1.303203\t valid mse:  1.4002540912947583\n",
      "Epoch 22  train mse: 1.2666914\t valid mse:  1.4007063261283177\n",
      "Epoch 23  train mse: 1.2549918\t valid mse:  1.4003930742901587\n",
      "Epoch 24  train mse: 1.2999427\t valid mse:  1.3969681092549602\n",
      "Epoch 25  train mse: 1.2792584\t valid mse:  1.3961169172502967\n",
      "Epoch 26  train mse: 1.3030952\t valid mse:  1.3960328181509851\n",
      "Epoch 27  train mse: 1.2966789\t valid mse:  1.3969278116043256\n",
      "Epoch 28  train mse: 1.2921448\t valid mse:  1.3951838648251307\n",
      "Epoch 29  train mse: 1.2749981\t valid mse:  1.3948003259302144\n",
      "Epoch 30  train mse: 1.2736427\t valid mse:  1.3932985757470415\n",
      "Epoch 31  train mse: 1.2424577\t valid mse:  1.3975893895298879\n",
      "Epoch 32  train mse: 1.2691902\t valid mse:  1.3938091172278864\n",
      "Epoch 33  train mse: 1.2882614\t valid mse:  1.3925143114363314 mse: 1.3031929  train mse: 1.2811968\n",
      "Epoch 34  train mse: 1.2703928\t valid mse:  1.3930047343696084\n",
      "Epoch 35  train mse: 1.2706822\t valid mse:  1.3916532957796668\n",
      "Epoch 36  train mse: 1.2685767\t valid mse:  1.39194040349422\n",
      "Epoch 37  train mse: 1.2508096\t valid mse:  1.3921981492959241\n",
      "Epoch 38  train mse: 1.2959521\t valid mse:  1.3914830812802557\n",
      "Epoch 39  train mse: 1.2840945\t valid mse:  1.3899454938695324\n",
      "Epoch 40  train mse: 1.2675403\t valid mse:  1.3909428539373245\n",
      "Epoch 41  train mse: 1.2752292\t valid mse:  1.3914720719059526\n",
      "Epoch 42  train mse: 1.2612197\t valid mse:  1.3919919189505425\n",
      "Epoch 43  train mse: 1.2868989\t valid mse:  1.3896154736764772\n",
      "Epoch 44  train mse: 1.2648116\t valid mse:  1.3903085530506372\n",
      "Epoch 45  train mse: 1.2351686\t valid mse:  1.3915893578450604\n",
      "Epoch 46  train mse: 1.2496358 46  train mse: 1.2674057\t valid mse:  1.3897612979699858\n",
      "Epoch 47  train mse: 1.2899562\t valid mse:  1.3895430860067124\n",
      "Epoch 48  train mse: 1.2671266\t valid mse:  1.392075502206243\n",
      "Epoch 49  train mse: 1.2421573\t valid mse:  1.3923435288100212\n",
      "Epoch 50  train mse: 1.283805\t valid mse:  1.390068736757991\n",
      "Epoch 51  train mse: 1.2398309\t valid mse:  1.3901633921627417\n",
      "Epoch 52  train mse: 1.2505763\t valid mse:  1.3894135124359006: 1.2621665\n",
      "Epoch 53  train mse: 1.2768062\t valid mse:  1.389184469511629\n",
      "Epoch 54  train mse: 1.288897\t valid mse:  1.3903529205750724\n",
      "Epoch 55  train mse: 1.2856487\t valid mse:  1.3890011086020781\n",
      "Epoch 56  train mse: 1.2403352\t valid mse:  1.3913686166439643\n",
      "Epoch 57  train mse: 1.2686075\t valid mse:  1.3893456989149888\n",
      "Epoch 58  train mse: 1.2851915\t valid mse:  1.3875950405830564\n",
      "Epoch 59  train mse: 1.2470889\t valid mse:  1.389911295176687\n",
      "Epoch 60  train mse: 1.292167360  train mse: 1.2826334\t valid mse:  1.3890152093221015\n",
      "Epoch 61  train mse: 1.2817651\t valid mse:  1.3883482937601932\n",
      "Epoch 62  train mse: 1.3096077\t valid mse:  1.3867651974861515\n",
      "Epoch 63  train mse: 1.2635981\t valid mse:  1.3891135502879566\n",
      "Epoch 64  train mse: 1.2974591\t valid mse:  1.3903548542682462\n",
      "Epoch 65  train mse: 1.26265144\t valid mse:  1.3893019512178273\n",
      "Epoch 66  train mse: 1.2810811\t valid mse:  1.3884094601987367\n",
      "Epoch 67  train mse: 1.23143844\t valid mse:  1.3903773501149352\n",
      "Epoch 68  train mse: 1.2730362\t valid mse:  1.388586801498805\n",
      "Epoch 69  train mse: 1.2682439 1.2238035\t valid mse:  1.388950665471601\n",
      "Epoch 70  train mse: 1.2595004\t valid mse:  1.389604119979624\n",
      "Epoch 71  train mse: 1.2754579\t valid mse:  1.3884333797531874\n",
      "Epoch 72  train mse: 1.2537564\t valid mse:  1.3876988351906379\n",
      "Epoch 73  train mse: 1.2325943\t valid mse:  1.3882634978347321\n",
      "Epoch 74  train mse: 1.2889354\t valid mse:  1.3866073156526506\n",
      "Epoch 75  train mse: 1.2520344\t valid mse:  1.388623596500595\n",
      "Epoch 76  train mse: 1.2815306\t valid mse:  1.3890960035113562\n",
      "Epoch 77  train mse: 1.301135\t valid mse:  1.3892106651537812\n",
      "Epoch 78  train mse: 1.2744313\t valid mse:  1.3866164207940315\n",
      "Epoch 79  train mse: 1.2564224\t valid mse:  1.3879533175284777\n",
      "Epoch 80  train mse: 1.2909249\t valid mse:  1.3872391479832942\n",
      "Epoch 81  train mse: 1.3006506\t valid mse:  1.3861934521174915\n",
      "Epoch 82  train mse: 1.2878758\t valid mse:  1.386877850143317\n",
      "Epoch 83  train mse: 1.2911272\t valid mse:  1.3862685647326574\n",
      "Epoch 84  train mse: 1.2977579\t valid mse:  1.3861356667124927\n",
      "Epoch 85  train mse: 1.2939751\t valid mse:  1.386012718573271\n",
      "Epoch 86  train mse: 1.252193\t valid mse:  1.3882727983234018\n",
      "Epoch 87  train mse: 1.2756807\t valid mse:  1.387936618163677\n",
      "Epoch 88  train mse: 1.2763826\t valid mse:  1.3871595203401914\n",
      "Epoch 89  train mse: 1.2781286 train mse: 1.2668244\t valid mse:  1.3879407652170217\n",
      "Epoch 90  train mse: 1.2585105\t valid mse:  1.3867943688811022\n",
      "Epoch 91  train mse: 1.2644451\t valid mse:  1.3869269827148116\n",
      "Epoch 92  train mse: 1.2654916\t valid mse:  1.387560271453111\n",
      "Epoch 93  train mse: 1.2754041\t valid mse:  1.387036923558061\n",
      "Epoch 94  train mse: 1.2876475\t valid mse:  1.387120125890949\n",
      "Epoch 95  train mse: 1.2581149 train mse: 1.318577995  train mse: 1.2529553 95  train mse: 1.2519504\t valid mse:  1.3884058877480399\n",
      "Epoch 96  train mse: 1.2498691\t valid mse:  1.3867589666593676\n",
      "Epoch 97  train mse: 1.2798665  train mse: 1.2843103\t valid mse:  1.3863184934843549\n",
      "Epoch 98  train mse: 1.2718294\t valid mse:  1.386046037975377\n",
      "Epoch 99  train mse: 1.2497406\t valid mse:  1.3868027979115989\n"
     ]
    }
   ],
   "source": [
    "# 1. batch 遍历训练集 metric\n",
    "#    1.1 自动求导\n",
    "# 2. epoch结束 验证集 metric\n",
    "\n",
    "epochs = 100\n",
    "batch_size = 32\n",
    "steps_per_epoch = len(x_train_scaled) // batch_size\n",
    "optimizer = keras.optimizers.SGD()\n",
    "metric = keras.metrics.MeanSquaredError()\n",
    "\n",
    "def random_batch(x, y, batch_size=32):\n",
    "    idx = np.random.randint(0, len(x), size=batch_size)\n",
    "    return x[idx], y[idx]\n",
    "\n",
    "model = keras.models.Sequential([\n",
    "    keras.layers.Dense(30, activation='relu',\n",
    "                       input_shape=x_train.shape[1:]),\n",
    "    keras.layers.Dense(1),\n",
    "])\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    metric.reset_states()\n",
    "    for step in range(steps_per_epoch):\n",
    "        x_batch, y_batch = random_batch(x_train_scaled, y_train,\n",
    "                                        batch_size)\n",
    "        with tf.GradientTape() as tape:\n",
    "            y_pred = model(x_batch)\n",
    "            loss = tf.reduce_mean(\n",
    "                keras.losses.mean_squared_error(y_batch, y_pred))\n",
    "            metric(y_batch, y_pred)\n",
    "        grads = tape.gradient(loss, model.variables)\n",
    "        grads_and_vars = zip(grads, model.variables)\n",
    "        optimizer.apply_gradients(grads_and_vars)\n",
    "        print(\"\\rEpoch\", epoch, \" train mse:\",\n",
    "              metric.result().numpy(), end=\"\")\n",
    "    y_valid_pred = model(x_valid_scaled)\n",
    "    valid_loss = tf.reduce_mean(\n",
    "        keras.losses.mean_squared_error(y_valid_pred, y_valid))\n",
    "    print(\"\\t\", \"valid mse: \", valid_loss.numpy())\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
